<template>
  <body>
  <div class="main">

    <div class="container a-container" id="a-container">
      <form class="form" id="a-form" method="" action="">
        <h2 class="form_title title">创建账号</h2>
        <span class="form__span">or use email for registration</span>
        <input class="form__input" type="text" placeholder="account" v-model="registerForm.account">
        <input class="form__input" type="password" placeholder="password" v-model="registerForm.password">
        <input class="form__input" type="password" placeholder="checkedPassword" v-model="registerForm.checkPassword">
        <button class="form__button button submit" @click="userRegister">立即注册</button>
      </form>
    </div>


    <div class="container b-container" id="b-container">
      <form class="form" id="b-form" method="" action="">
        <h2 class="form_title title">登录网站</h2>

        <span class="form__span">or use your email account</span>
        <input class="form__input" type="text" placeholder="account" v-model="loginForm.account">
        <input class="form__input" type="password" placeholder="Password" v-model="loginForm.password">
        <a class="form__link">忘记密码?</a>
        <button class="form__button button submit" @click="login">登录</button>
      </form>
    </div>


    <div class="switch" id="switch-cnt">
      <div class="switch__circle"></div>
      <div class="switch__circle switch__circle--t"></div>
      <div class="switch__container" id="switch-c1">
        <h2 class="switch__title title">你好朋友 !</h2>
        <p class="switch__description description">与我们保持联系,请登录您的个人信息</p>
        <button class="switch__button button switch-btn">立即登录</button>
      </div>
      <div class="switch__container is-hidden" id="switch-c2">
        <h2 class="switch__title title">欢迎回来 !</h2>
        <p class="switch__description description">输入您的个人信息,并开始与我们沟通</p>
        <button class="switch__button button switch-btn">立即注册</button>
      </div>
    </div>


  </div>
  </body>

</template>

<script setup>
import {onMounted, ref} from 'vue'
import {useUserStore} from "@/stores/userStore.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";

const userStore = useUserStore()


const loginForm = ref({
  account: 'admin',
  password: '123456',
})

const registerForm = ref({
  account: '',
  password: '',
  checkPassword: ''
})


const validateAccount = (account) => {
  const accountRegex = /^[a-zA-Z0-9]{6,}$/;
  if (account.length < 6) return "账号不能少于6位";
  if (!accountRegex.test(account)) return "账号不能包含特殊字符";
  return true;
};

const validatePassword = (password) => {
  const passwordRegex = /^(?=.*[A-Z])(?=.*[!@#$%^&*]).{8,}$/;
  if (password.length < 8) return "密码不能少于8位";
  if (!passwordRegex.test(password)) return "密码必须包含至少一个大写字母和一个特殊字符";
  return true;
};

const userRegister = async () => {
  // 账号验证
  const accountValidation = validateAccount(registerForm.value.account);
  if (accountValidation !== true) {
    ElMessage.error(accountValidation);
    return;
  }

  // 密码验证
  const passwordValidation = validatePassword(registerForm.value.password);
  if (passwordValidation !== true) {
    ElMessage.error(passwordValidation);
    return;
  }

  // 确认密码验证
  if (registerForm.value.password !== registerForm.value.checkPassword) {
    ElMessage.error('两次输入的密码不一致');
    return;
  }

  try {
    console.log("account", registerForm.value.account)
    console.log("password", registerForm.value.password)
    console.log("checkPassword", registerForm.value.checkPassword)
    console.log("registerForm", registerForm.value)
    const res = await userStore.userRegister(
        registerForm.value.account,
        registerForm.value.password,
        registerForm.value.checkPassword
    );

    if (res.data.code === 200) {
      ElMessage.success('注册成功');
      router.push('/');
    } else {
      ElMessage.error(res.data.message || '注册失败');
    }
  } catch (error) {
    ElMessage.error('注册请求失败');
    console.error(error);
  }
};

const login = async () => {
  // 账号格式验证
  const accountValidation = validateAccount(loginForm.value.account);
  if (accountValidation !== true) {
    ElMessage.error(accountValidation);
    return;
  }

  try {
    const res = await userStore.userLogin(
        loginForm.value.account,
        loginForm.value.password
    );

    if (res.data.code === 200) {
      ElMessage.success('登录成功');
      router.push('/');
    } else {
      ElMessage.error(res.data.message || '登录失败');
    }
  } catch (error) {
    ElMessage.error('登录请求失败');
    console.error(error);
  }
};


onMounted(() => {
  let switchCtn = document.querySelector("#switch-cnt");
  let switchC1 = document.querySelector("#switch-c1");
  let switchC2 = document.querySelector("#switch-c2");
  let switchCircle = document.querySelectorAll(".switch__circle");
  let switchBtn = document.querySelectorAll(".switch-btn");
  let aContainer = document.querySelector("#a-container");
  let bContainer = document.querySelector("#b-container");
  let allButtons = document.querySelectorAll(".submit");

  let getButtons = (e) => e.preventDefault();

  let changeForm = (e) => {
    switchCtn.classList.add("is-gx");
    setTimeout(() => {
      switchCtn.classList.remove("is-gx");
    }, 1500);

    switchCtn.classList.toggle("is-txr");
    switchCircle[0].classList.toggle("is-txr");
    switchCircle[1].classList.toggle("is-txr");

    switchC1.classList.toggle("is-hidden");
    switchC2.classList.toggle("is-hidden");
    aContainer.classList.toggle("is-txl");
    bContainer.classList.toggle("is-txl");
    bContainer.classList.toggle("is-z200");
  };

  for (let i = 0; i < allButtons.length; i++) {
    allButtons[i].addEventListener("click", getButtons);
  }

  for (let i = 0; i < switchBtn.length; i++) {
    switchBtn[i].addEventListener("click", changeForm);
  }
});

</script>


<style scoped>

*, *::after, *::before {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  user-select: none;
}

/* Generic */
body {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: 'Montserrat', sans-serif;
  font-size: 12px;
  background-color: #ecf0f3;
  color: #a0a5a8;
}

/**/
.main {
  position: relative;
  width: 1000px;
  min-width: 1000px;
  min-height: 600px;
  height: 600px;
  padding: 25px;
  background-color: #ecf0f3;
  box-shadow: 10px 10px 10px #d1d9e6, -10px -10px 10px #f9f9f9;
  border-radius: 12px;
  overflow: hidden;
}

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0;
  width: 600px;
  height: 100%;
  padding: 25px;
  background-color: #ecf0f3;
  transition: 1.25s;
}

.form {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  width: 100%;
  height: 100%;
}

.form__icon {
  object-fit: contain;
  width: 30px;
  margin: 0 5px;
  opacity: .5;
  transition: .15s;
}

.form__icon:hover {
  opacity: 1;
  transition: .15s;
  cursor: pointer;
}

.form__input {
  width: 350px;
  height: 40px;
  margin: 4px 0;
  padding-left: 25px;
  font-size: 13px;
  letter-spacing: .15px;
  border: none;
  outline: none;
  font-family: 'Montserrat', sans-serif;
  background-color: #ecf0f3;
  transition: .25s ease;
  border-radius: 8px;
  box-shadow: inset 2px 2px 4px #d1d9e6, inset -2px -2px 4px #f9f9f9;
}

.form__input:focus {
  box-shadow: inset 4px 4px 4px #d1d9e6, inset -4px -4px 4px #f9f9f9;
}

.form__span {
  margin-top: 30px;
  margin-bottom: 12px;
}

.form__link {
  color: #181818;
  font-size: 15px;
  margin-top: 25px;
  border-bottom: 1px solid #a0a5a8;
  line-height: 2;
}

.title {
  font-size: 34px;
  font-weight: 700;
  line-height: 3;
  color: #181818;
}

.description {
  font-size: 14px;
  letter-spacing: .25px;
  text-align: center;
  line-height: 1.6;
}

.button {
  width: 180px;
  height: 50px;
  border-radius: 25px;
  margin-top: 50px;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 1.15px;
  background-color: #4B70E2;
  color: #f9f9f9;
  box-shadow: 8px 8px 16px #d1d9e6, -8px -8px 16px #f9f9f9;
  border: none;
  outline: none;
}

/**/
.a-container {
  z-index: 100;
  left: calc(100% - 600px);
}

.b-container {
  left: calc(100% - 600px);
  z-index: 0;
}

.switch {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 400px;
  padding: 50px;
  z-index: 200;
  transition: 1.25s;
  background-color: #ecf0f3;
  overflow: hidden;
  box-shadow: 4px 4px 10px #d1d9e6, -4px -4px 10px #f9f9f9;
}

.switch__circle {
  position: absolute;
  width: 500px;
  height: 500px;
  border-radius: 50%;
  background-color: #ecf0f3;
  box-shadow: inset 8px 8px 12px #d1d9e6, inset -8px -8px 12px #f9f9f9;
  bottom: -60%;
  left: -60%;
  transition: 1.25s;
}

.switch__circle--t {
  top: -30%;
  left: 60%;
  width: 300px;
  height: 300px;
}

.switch__container {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  position: absolute;
  width: 400px;
  padding: 50px 55px;
  transition: 1.25s;
}

.switch__button {
  cursor: pointer;
}

.switch__button:hover {
  box-shadow: 6px 6px 10px #d1d9e6, -6px -6px 10px #f9f9f9;
  transform: scale(0.985);
  transition: .25s;
}

.switch__button:active, .switch__button:focus {
  box-shadow: 2px 2px 6px #d1d9e6, -2px -2px 6px #f9f9f9;
  transform: scale(0.97);
  transition: .25s;
}

/**/
.is-txr {
  left: calc(100% - 400px);
  transition: 1.25s;
  transform-origin: left;
}

.is-txl {
  left: 0;
  transition: 1.25s;
  transform-origin: right;
}

.is-z200 {
  z-index: 200;
  transition: 1.25s;
}

.is-hidden {
  visibility: hidden;
  opacity: 0;
  position: absolute;
  transition: 1.25s;
}

.is-gx {
  animation: is-gx 1.25s;
}

@keyframes is-gx {
  0%, 10%, 100% {
    width: 400px;
  }
  30%, 50% {
    width: 500px;
  }
}
</style>
